package cn.ghx.tool.novel.grabber;

import cn.ghx.tool.novel.domain.Book;
import cn.ghx.tool.novel.domain.Caption;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GrabberXinguli extends GrabberBase{

    private static final String URI = "http://www.xinguli.com";

    GrabberXinguli(String url, String path) {
        super(url,path);
    }

    @Override
    protected Book getBookWithoutContent(Document doc) {
        Book book = new Book();
        String bookTitle = doc.select("meta[property=og:title]").get(0).attr("content");
        String author = doc.select("meta[property=og:novel:author]").get(0).attr("content");

        book.setTitle(bookTitle);
        book.setAuthor(author);

        Elements links = doc.select("#detaillist ul li a");

        if (links.size() > 0) {
            for (Element a : links){
                String title = a.attr("title");
                String link = URI + a.attr("href");
                Caption cap = new Caption(title,link);
                book.getCaptions().add(cap);
            }

        }
        return book;
    }

    @Override
    public String getCaptionContent(String link) {
        try {
            Document doc = Jsoup.connect(link).get();
            Element el = doc.getElementById("content");
            return el.ownText()
                    .replace("一秒记住【新谷粒小说网 www.xinguli.com】，精彩小说无弹窗免费阅读！","")
                    .replace(" ","")
                    .replaceAll(" ","");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
